// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-26 15:32
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DateTrain;

import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;
import java.util.Deque;

@SuppressWarnings("ALL")
public class CalPoints {
    /**
     * 棒球比赛
     * 栈操作
     */
    public int calPoints(String[] ops) {
        /*-----栈定义-----*/
        Deque<Integer> deque = new ArrayDeque<>();
        for (String op : ops) {
            if ("C".equals(op)) {
                deque.pollLast();
            } else if ("D".equals(op)) {
                deque.addLast(deque.getLast() * 2);
            } else if ("+".equals(op)) {
                int top = deque.pollLast();
                int tmp = top + deque.getLast();
                deque.addLast(top);
                deque.addLast(tmp);
            } else {
                deque.addLast(Integer.parseInt(op));
            }
        }
        int res = 0;
        for (Integer dq : deque) {
            res += dq;
        }
        return res;
    }

    @Test
    public void shout() {
        //String[] test = {"5", "2", "C", "D", "+"};
        String[] test = {"5", "-2", "4", "C", "D", "9", "+", "+"};
        System.out.println(calPoints(test));
    }
}
